//
//  QRCodePickerManager.m
//  YuanXinCamera
//
//  Created by 晏德智 on 2016/11/11.
//  Copyright © 2016年 晏德智. All rights reserved.
//

#import "ScanCodeViewManager.h"
#import "ScanCodeView.h"

#import <React/RCTBridge.h>
#import <React/RCTEventDispatcher.h>
#import <React/RCTUIManager.h>
#import <React/RCTUtils.h>
#import "UIView+React.h"
#import <AVFoundation/AVFoundation.h>

@interface ScanCodeViewManager()

@end

@implementation ScanCodeViewManager

RCT_EXPORT_MODULE()

/**
 * 是否自动开启播放
 */
RCT_EXPORT_VIEW_PROPERTY(autoStartRunning, BOOL)
RCT_EXPORT_VIEW_PROPERTY(contentInset, UIEdgeInsets)
RCT_EXPORT_VIEW_PROPERTY(scanSize,CGSize)
/**
 * 读取扫码结果
 */
RCT_EXPORT_VIEW_PROPERTY(onCodeRead, RCTDirectEventBlock)
/**
 * 发生错误时触发
 */
RCT_EXPORT_VIEW_PROPERTY(onError, RCTDirectEventBlock)

/**
 * 设置描框的背景颜色
 */
RCT_EXPORT_VIEW_PROPERTY(maskViewBackgroundColor, UIColor)

/**
 * 设置边框的颜色
 */
RCT_EXPORT_VIEW_PROPERTY(maskViewBorderColorColor, UIColor)

- (dispatch_queue_t)methodQueue
{
    return dispatch_get_main_queue();
}

- (UIView *)view
{
    ScanCodeView *scanCode =  [[ScanCodeView alloc] init];
    return scanCode;
}

- (NSDictionary *)constantsToExport
{
    return @{
             @"BarCodeType": @{
                     @"upce": AVMetadataObjectTypeUPCECode,
                     @"code39": AVMetadataObjectTypeCode39Code,
                     @"code39mod43": AVMetadataObjectTypeCode39Mod43Code,
                     @"ean13": AVMetadataObjectTypeEAN13Code,
                     @"ean8":  AVMetadataObjectTypeEAN8Code,
                     @"code93": AVMetadataObjectTypeCode93Code,
                     @"code138": AVMetadataObjectTypeCode128Code,
                     @"pdf417": AVMetadataObjectTypePDF417Code,
                     @"qr": AVMetadataObjectTypeQRCode,
                     @"aztec": AVMetadataObjectTypeAztecCode,
                     @"datamatrix": AVMetadataObjectTypeDataMatrixCode
                     },
            
             };
}

RCT_EXPORT_METHOD(start:(nonnull NSNumber *)reactTag)
{
    [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTView *> *viewRegistry) {
        ScanCodeView *view = viewRegistry[reactTag];
        if (![view isKindOfClass:[ScanCodeView class]]) {
            RCTLogError(@"Invalid view returned from registry, expecting ScanCodeView, got: %@", view);
        } else {
            [view startRunning];
        }
    }];
}

RCT_EXPORT_METHOD(stop:(nonnull NSNumber *)reactTag)
{
    [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTView *> *viewRegistry) {
        ScanCodeView *view = viewRegistry[reactTag];
        if (![view isKindOfClass:[ScanCodeView class]]) {
            RCTLogError(@"Invalid view returned from registry, expecting ScanCodeView, got: %@", view);
        } else {
            [view stopRunning];
        }
    }];
}

RCT_EXPORT_METHOD(flashOpen:(nonnull NSNumber *)reactTag)
{
    [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTView *> *viewRegistry) {
        ScanCodeView *view = viewRegistry[reactTag];
        if (![view isKindOfClass:[ScanCodeView class]]) {
            RCTLogError(@"Invalid view returned from registry, expecting ScanCodeView, got: %@", view);
        } else {
            [view flashOpen];
        }
    }];
}

RCT_EXPORT_METHOD(flashClose:(nonnull NSNumber *)reactTag)
{
    [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTView *> *viewRegistry) {
        ScanCodeView *view = viewRegistry[reactTag];
        if (![view isKindOfClass:[ScanCodeView class]]) {
            RCTLogError(@"Invalid view returned from registry, expecting ScanCodeView, got: %@", view);
        } else {
            [view flashClose];
        }
    }];
}

@end
